﻿using UnityEngine;
using System.Collections;

public delegate void ClickDelegate(); 
public class Button : MonoBehaviour {

    public enum PositionHorizontal {Left,Right,Center};
    public string Name;
    public enum PositionVertical { Top, Bottom, Center };    

    public PositionHorizontal Horizontal;
    public PositionVertical Vertical = PositionVertical.Center;
    public Rect MarginSize=new Rect(0,0,60,60);
    private Rect TouchZone;

    private Rect PixelInsetNew;

  //  public bool IsPressed = false;
    private Color tmpclr;

    public bool TransparentMode = true;

    public bool AutoSize = false;
    public float SizePCT = 10;
    public bool X = false;
    public bool Y = false;

    public event ClickDelegate ButtonClick; 
    
    private GUITexture gui;
	void Start () {   
       
        gui = (GUITexture)GetComponent(typeof(GUITexture));

       
        ChangeSettings();
	}
	
    public void ChangeSettings()
    {
        if (AutoSize)
        {
            MarginSize.height = Screen.height / 100.0f * SizePCT;
            MarginSize.width = MarginSize.height;
            if (X)
            {
                MarginSize.x = MarginSize.height - 5;
            }
            if(Y)
            {
                MarginSize.y = MarginSize.height - 5;
            }
        }

        PixelInsetNew = MarginSize;
        switch (Horizontal)
        {
            case PositionHorizontal.Left:
                PixelInsetNew.x = MarginSize.x;
                break;
            case PositionHorizontal.Right:
                PixelInsetNew.x = Screen.width - MarginSize.x - MarginSize.width;
                break;
            default:
                PixelInsetNew.x = (Screen.width - MarginSize.width - MarginSize.x)/2;
                break;
        }

        switch (Vertical)
        {
            case PositionVertical.Bottom:
                PixelInsetNew.y = MarginSize.y;
                break;
            case PositionVertical.Top:
                PixelInsetNew.y = Screen.height - MarginSize.y - MarginSize.height;
                break;
            default:
                PixelInsetNew.y = (Screen.height - MarginSize.height - MarginSize.y) / 2;
                break;
        }

        gui.pixelInset = PixelInsetNew;
        TouchZone = gui.pixelInset;
    }
	
	void Update () {   
     
        // ЭТУ СТРОЧКУ НАДО УДАЛИТЬ !!!!!!!!
        // ChangeSettings();
        // ---------------------------------


        int count = Input.touchCount;
        for (int i = 0; i < count; i++)
        {

            Touch touch = Input.GetTouch(i);

            if (TouchZone.Contains(touch.position))
            {
                if (touch.phase == TouchPhase.Began)
                {
                    //IsPressed = true;
                    if (ButtonClick != null)
                    {
                        ButtonClick();
                    }
                }
                if (TransparentMode)
                {
                    tmpclr = gui.color;
                    tmpclr.a = 0.9f;
                    gui.color = tmpclr;
                }
            }
            else
            {
                Reset();
            }
            

        }
        if (Input.touchCount == 0)
        {
            Reset();
        }
	}

    public void Reset()
    {
          //IsPressed = false;
        if (TransparentMode)
        {
            tmpclr = gui.color;
            tmpclr.a = 0.2f;
            gui.color = tmpclr;
        }
     
    }
}
